﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.Shell;
using AxlfVSX.Core;
using EnvDTE;
using Aoxuelf.MDSTool.UI;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace Aoxuelf.MDSTool.Commands
{
    [CommandID(GuidList.guidMDSToolCmdSetString, PkgCmdIDList.cmdidCopySqlPathFunc)]
    public class CopySqlPathCommand : DynamicCommand
    {
        public CopySqlPathCommand(Package package)
            : base(package)
        {
        }

        protected override bool CanExecute(OleMenuCommand command)
        {
            try
            {
                if (FileCodeModelHelper.IsDataAccessClass(Dte))
                {
                    CodeFunction func = FileCodeModelHelper.GetCodeFunction(Dte);
                    if (func != null)
                    {
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }

            return false;
        }

        protected override void OnExecute(OleMenuCommand command)
        {
            try
            {
                TextSelection textSel = Dte.ActiveDocument.Selection as TextSelection;
                textSel.SelectLine();
                string text = textSel.Text;
                if (text.IndexOf('"') >= 0)
                {
                    int firstIndex = text.IndexOf('"');
                    int endIndex = text.LastIndexOf('"');

                    string sqlPath = text.Substring(firstIndex + 1, endIndex - firstIndex - 1);
                    string result = "<SQLConfig>" + sqlPath + "</SQLConfig>";
                    Clipboard.SetText(result, TextDataFormat.UnicodeText);
                    MessageBox.Show("已复制到剪贴板！");

                    MDSOutPaneWindow.MDSTool.Clear();
                    MDSOutPaneWindow.MDSTool.WriteLine(result);
                    MDSOutPaneWindow.MDSTool.Activate();
                }
            }
            catch (Exception ex)
            {
                MDSOutPaneWindow.MDSTool.WriteLine(ex.Message);
                MDSOutPaneWindow.MDSTool.Activate();
            }
        }
    }
}
